package com.testMvc.custMvc;

import com.alibaba.fastjson.JSONObject;
import com.core.comment.StringUtil;
import com.core.jdbc.MySqlSession;
import com.core.netty.http.work.HttpMyReponse;
import com.core.netty.http.work.HttpMyRequest;
import com.core.redis.RedisClient;
import com.core.safe.MySubject;
import com.core.safe.UserToken;
import com.core.springmvc.MyController;
import com.core.springmvc.MyRequestMapping;

import java.util.HashMap;
import java.util.Map;

@MyController
@MyRequestMapping("/CustAction")
public class CustAction {

    @MyRequestMapping("/getCustList")
    public void getCustList(HttpMyRequest req, HttpMyReponse rep) {
        CustDao cd = MySqlSession.getMapper(CustDao.class);
        Map<String, Object> par = new HashMap<>();
        par.put("name", "%杜%");
        Map<String, Object> result = new HashMap<>();
        result.put("data", cd.getCustList(par));
        rep.fullWrite(JSONObject.toJSONString(result));

    }

    @MyRequestMapping("/login")
    public void loing(HttpMyRequest req, HttpMyReponse rep) {
        UserToken userToken = new UserToken("kk", req.getParameter("password"));
        try {
            MySubject subject = MySubject.getInstance(req.getRemoteHost());
            String token = subject.login(userToken);
            System.out.println("登录成功:" + token);
            rep.fullWrite(JSONObject.toJSONString("登录成功:" + token));
        } catch (Exception e) {
            e.printStackTrace();
            rep.fullWrite(e.getMessage());
        }

    }

    @MyRequestMapping("/testRedis")
    public void testRedis(HttpMyRequest req, HttpMyReponse rep) {
        UserToken userToken = new UserToken("kk", req.getParameter("password"));
        try {
            String key = StringUtil.getUuid();
            String value = StringUtil.getUuid();
            RedisClient.set(key, value);
            System.out.println(RedisClient.get(key));
            rep.fullWrite("成功");
        } catch (Exception e) {
            e.printStackTrace();
            rep.fullWrite(e.getMessage());

        }

    }
}
